package com.sincetimes.game.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;

public class ConfigManager{

	private static ConfigManager manager = new ConfigManager();
	private HashMap<String, String> configMap = new HashMap<String, String>();
	private Log logger = LogFactory.getLog(ConfigManager.class);

	public static ConfigManager getInstance() {
		return manager;
	}

	private ConfigManager() {
		loadConfig();
	}

	public String getConfig(String name) {
		return configMap.get(name);
	}

	public void setConfig(String name, String value) {
		configMap.put(name, value);
	}

	public void loadConfig() {
		try {
			ClassPathResource resource = new ClassPathResource("game.properties");
			Properties properties = new Properties();
			properties.load(resource.getInputStream());
			Enumeration<?> en = properties.propertyNames();
			
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String property = properties.getProperty(key);
				System.out.println(key+"="+property);
				setConfig(key, property);
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}
	
	public Map getAllConfig() {
		return configMap;
	}
}
